import { PrismaService } from '@app/prisma';
import { SysFeedbackEntity } from '@app/prisma/sys.feedback.entity/sys.feedback.entity';
import { SysUserEntity } from '@app/prisma/sys.user.entity/sys.user.entity';
import { UseGuards } from '@nestjs/common';
import { Args, Int, Mutation, Query, Resolver } from '@nestjs/graphql';
import { DELETE_POWER, GqlAuthGuard, GqlCurrentUser, MakeGqlAuthPowerGuard, UPDATE_POWER, VIEW_POWER } from 'src/auth/auth.guard';


export const FeedbackGuard = MakeGqlAuthPowerGuard("/feedback", "反馈管理")
@Resolver()
export class FeedbackResolver {
  constructor(
    private readonly prisma: PrismaService
  ) { }


  /**
   * 用户提交错误
   * @param id sys_test_questionId
   * @param desc 纠错内容
   */
  @Mutation(() => Int)
  @UseGuards(GqlAuthGuard)
  async submitError(
    @Args("id", { type: () => Int }) id: number,
    @Args("desc", { type: () => String }) desc: string,
    @GqlCurrentUser() user: SysUserEntity
  ) {
    if (!await this.prisma.sys_test_question.findUnique({ where: { id } })) throw new Error("题目不存在");
    return await this.prisma.sys_feedback.create({
      data: {
        sys_user: {
          connect: {
            id: user.id
          }
        },
        desc,
        sys_test_question: {
          connect: {
            id
          }
        }
      }
    })

  }

  /**
   * 查看纠错 
   */
  @Query(() => [SysFeedbackEntity])
  @UseGuards(GqlAuthGuard)
  async querySqlfCorrection(
    @GqlCurrentUser() user: SysUserEntity
  ) {
    return await this.prisma.sys_feedback.findMany({
      where: {
        sys_user: {
          id: user.id
        }
      }
    })
  }

  /**
   * 删除自己的纠错
   */
  @Mutation(() => Int)
  @UseGuards(GqlAuthGuard)
  async deleteSelfCorrection(
    @GqlCurrentUser() user: SysUserEntity,
    @Args("id", { type: () => Int }) id: number
  ) {
    const feedback = await this.prisma.sys_feedback.findUnique({ where: { id } })
    if (!feedback) throw new Error("纠错不存在");
    if (feedback.sys_userId !== user.id) throw new Error("你删别人的纠错？？？");
    return await this.prisma.sys_feedback.delete({ where: { id } })
  }

  /**
   *  * 管理员查看纠错
   */
  @Query(() => [SysFeedbackEntity])
  @UseGuards(FeedbackGuard([VIEW_POWER]))
  async queryCorrection() {
    return await this.prisma.sys_feedback.findMany()
  }

  /**
   * 回复
   */
  @Mutation(() => Int)
  @UseGuards(FeedbackGuard([UPDATE_POWER]))
  async replyCorrection(
    @Args("id", { type: () => Int }) id: number,
    @Args("reply", { type: () => String }) reply: string,
  ) {
    const feedback = await this.prisma.sys_feedback.findUnique({ where: { id } })
    if (!feedback) throw new Error("纠错不存在");
    return await this.prisma.sys_feedback.update({
      where: { id },
      data: { reply }
    })
  }

  /**
   * 删除纠错
   */
  @Mutation(() => Int)
  @UseGuards(FeedbackGuard([DELETE_POWER]))
  async deleteCorrection(
    @Args("id", { type: () => Int }) id: number,
  ) {
    const feedback = await this.prisma.sys_feedback.findUnique({ where: { id } })
    if (!feedback) throw new Error("纠错不存在");
    return await this.prisma.sys_feedback.delete({ where: { id } })
  }
}
